package com.chixing.ih.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HyperLogLogOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * 目的：模板
 * PageViewTotalUtil.SetRedisCache("zzz/", 100, req.getRemoteAddr());
 * Long cache = PageViewTotalUtil.GetRedisCache("zzz/", 100);
 */

/**
 * 目的: 为了统计浏览量
 *
 * @author ：zcw
 * @version: 1.0  2020/9/18 10:16
 */
@Component
public class PageViewTotalUtil {


    private static RedisTemplate<String, Object> redisTemplate;

    @Autowired
    public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
        PageViewTotalUtil.redisTemplate = redisTemplate;
    }

    /**
     * @description: 添加浏览记录
     * @param: String key: 访问端口，前面不要‘/’
     * Integer id: 唯一标识，任务模块，任务id，程序员列表模块，程序员id
     * String ip: 访问的ip地址
     * @return: void
     * @author: zcw
     * @date: 2020/9/18
     */
    public static Boolean SetRedisCache(Long id) {
        ListOperations<String, Object> ops = redisTemplate.opsForList();

        try {
            ops.rightPush(id + "", new Date());
            return true;
        } catch (Exception e) {
            return false;
        }
    }


    /**
     * @description: 获取浏览量
     * @param: String key: 访问端口，前面不要‘/’
     * Integer id: 唯一标识，任务模块，任务id，程序员列表模块，程序员id
     * @return: Long
     * @author: zcw
     * @date: 2020/9/18
     */
    public static Long GetRedisCache(Long id) {
        ListOperations<String, Object> ops = redisTemplate.opsForList();
        return ops.size(id + "");
    }

}
